<?php
include("../../tools/session.php");
include("../../tools/controls.php");

// 如果登录失败、无权限访问，那么采取相应措施
if (!($is_login_success === true)) { session_hop_page("../index.php"); exit(); }
if (!power_check(0)) { include("tpl-inc-wp.php"); $tpl->display($oa_tpl_path . "/no-power.html"); exit(); }

$page_status = $_POST["page_status"];
$per_page = 10;
/*-- 以下是实际内容 --*/
if (!$page_status)
{
	include("tpl-inc-wp.php");
	$tpl->display($oa_tpl_path . "/default/doc_inquiry.html");
}
// 如果 $page_status == 1，那么进行搜索并显示数据
elseif ($page_status == 1)
{
	include("../../tools/mysql.php");
	db_connect();

	$page = $_POST["current_page"];
	$searchstr = $_POST["searchstr"];
	$instancy = $_POST["instancy"];
	$finishedflag = $_POST["finishedflag"];
	$passedflag = $_POST["passedflag"];
	$accessoryflag = $_POST["accessoryflag"];
	$applydate = $_POST["applydate"];
	
	$sstr = $searchstr;
	
	switch ($instancy)
	{
		case 1:		$ins = 1;		break;
		case 2:		$ins = 2;		break;
		case 3:		$ins = 3;		break;
		default:		$ins = -1;
	}
	switch ($finishedflag)
	{
		case 1:		$fin = 1;		break;
		case 2:		$fin = 0;		break;
		default:		$fin = -1;
	}
	switch ($passedflag)
	{
		case 1:		$pas = 1;		break;
		case 2:		$pas = 0;		break;
		default:		$pas = -1;
	}
	switch ($accessoryflag)
	{
		case 1:		$acc = 1;		break;
		case 2:		$acc = 0;		break;
		default:		$acc = -1;
	}
	switch ($applydate)
	{
		case "d":		$date_dis = 86400;			break;
		case "w":		$date_dis = 604800;		break;
		case "m":		$date_dis = 2628000;		break;
		case "m2":		$date_dis = 5256000;		break;
		case "m3":		$date_dis = 7884000;		break;
		case "m6":		$date_dis = 15768000;		break;
		case "y":		$date_dis = 31536000;		break;
		default:			$date_dis = -1;
	}
	
	$sql_str = array();
	$sql_str["ins"] = ($ins == -1) ? "" : " AND instancy=" . $ins;
	$sql_str["fin"] = ($fin == -1) ? "" : " AND is_finished=" . $fin;
	$sql_str["pas"] = ($pas == -1) ? "" : " AND is_passed_at_last=" . $pas;
	$sql_str["acc"] = ($acc == -1) ? "" : (($acc == 0) ? " AND accessory=''" : " AND accessory<>''");
	$sql_str["app"] = ($date_dis == -1) ? "" : " AND (UNIX_TIMESTAMP() - UNIX_TIMESTAMP(apply_time))<=" . $date_dis;
	
	// 获取符合条件的数据的个数
	$sql_num = "SELECT COUNT(doc_id) num FROM document WHERE (title LIKE '%" . $sstr . "%' OR content LIKE '%" . $sstr . "%')";
	foreach ($sql_str as $v) { $sql_num .= $v; }
	$sql_num .= " ORDER BY apply_time DESC";
	$re_num = db_query_once($sql_num);
	$items_num = $re_num["num"];
	
	$page = ($page == "" || $page < 0) ? 1 : ($page + 1);
	
	// 获取数据
	$sql = "SELECT * FROM document WHERE (title LIKE '%" . $sstr . "%' OR content LIKE '%" . $sstr . "%')";
	foreach ($sql_str as $v) { $sql .= $v; }
	$sql .= " ORDER BY apply_time DESC";
	$re = db_query_page($sql, $page, $per_page);
	
	header("content-type: text/xml");
	echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
	echo "<docs whole=\"" . $items_num . "\">\n";
	foreach ($re as $v)
	{
		echo "<item>\n";
		echo "<docid>" . $v["doc_id"] . "</docid>\n";
		echo "<title><![CDATA[" . $v["title"] . "]]></title>\n";
		echo "<instancy><![CDATA[" . getInstancy($v["instancy"]) . "]]></instancy>\n";
		echo "<applytime><![CDATA[" . $v["apply_time"] . "]]></applytime>\n";
		echo "<ismulti>" . $v["is_multi"] . "</ismulti>\n";
		echo "<issent>" . $v["is_sent"] . "</issent>\n";
		echo "<iscanceled>" . $v["is_canceled"] . "</iscanceled>\n";
		echo "<isfinished>" . $v["is_finished"] . "</isfinished>\n";
		echo "<ispassed>" . $v["is_passed_at_last"] . "</ispassed>";
		echo "</item>\n";
	}
	echo "</docs>\n";
}
// 如果 $page_status == 2
elseif ($page_status == 2)
{
	$doc_id = $_POST["doc_id"];
	include("../../tools/mysql.php");
	db_connect();
	
	header("content-type: text/xml");
	echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
	
	$sql = "SELECT content FROM document WHERE doc_id=" . $doc_id;
	$re = db_query_once($sql);
	
	echo "<doc>\n";
	echo "<content><![CDATA[" . $re["content"] . "]]></content>";
	echo "</doc>\n";
}


// 获取“紧急程度”
function getInstancy($ins)
{
	switch ($ins)
	{
		case 1:		return "一般"; 	break;
		case 2:		return "紧急";	break;
		case 3:		return "加急";	break;
		default:		return "一般";
	}
}
?>
